//
//  JZMFileManager.m
//  JZMMergeSDK
//
//  Created by zhl on 2017/2/4.
//  Copyright © 2017年 HLZ. All rights reserved.
//

#import "JZMFileManager.h"
#import "JZMExceptionModel.h"

@implementation JZMFileManager

+(NSString *)creatPlistFileWithArr{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0] stringByAppendingPathComponent:@"jftInfo.plist"];
    if ([[NSFileManager defaultManager] fileExistsAtPath:path] == NO)
        
    {
        NSFileManager* fileManager = [NSFileManager defaultManager];
        //if(IOS7)
//        [fileManager createFileAtPath:@"photo.plist" contents:nil attributes:nil];
        //if(IOS8)
        [fileManager createFileAtPath:path contents:nil attributes:nil];
        NSMutableArray *documentPathArray = [NSMutableArray array];
        
        [documentPathArray writeToFile:path atomically:YES];
        
    }
    
    return path;
}
+(void)addModel:(JZMExceptionModel *)model{
    NSDictionary *modelDic =[model properties_aps];
  NSMutableDictionary *dataDictionary = [[NSMutableDictionary alloc]initWithContentsOfFile:[self creatPlistFileWithArr]];
    if (!dataDictionary) {
        dataDictionary =[NSMutableDictionary dictionaryWithObjectsAndKeys:[@[]mutableCopy],@"info",nil];
    }
    NSMutableArray *mutableArray =[dataDictionary objectForKey:@"info"];
    [mutableArray addObject:modelDic];
    [dataDictionary setValue:mutableArray forKey:@"info"];
    //写入plist里面
    [dataDictionary writeToFile:[self creatPlistFileWithArr] atomically:YES];
}
+ (NSMutableDictionary *)readAllData{

       NSMutableDictionary *dataDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:[self creatPlistFileWithArr]];

    return dataDictionary;
}
+(void)deleteAll{
    NSFileManager *manager=[NSFileManager defaultManager];
    //文件路径
    NSString *filepath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"jftInfo.plist"];
    if ([manager removeItemAtPath:filepath error:nil]) {
//        NSLog(@"文件删除成功");
    }else{
//        NSLog(@"文件删除失败");

    }
}
+(void)changeDataWith:(JZMExceptionModel *)model{
    NSMutableDictionary *dic =[self readAllData];
//    NSLog(@"ordic=%@",dic);
    NSDictionary*modelDic =[model properties_aps];
    if (dic) {
        NSMutableArray *mutableArray =dic[@"info"];
        NSDictionary*item =[mutableArray lastObject];
        [mutableArray removeObject:item];
        [mutableArray addObject:modelDic];
        if ([dic writeToFile:[self creatPlistFileWithArr] atomically:YES]) {
//            NSLog(@"nowdic=%@",dic);
        }
    }
   
    
}
@end
